package client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import server.Commands;

public class JournalViewer extends JFrame implements ActionListener{
	private JTextArea textArea;
	private String file;
	private Connector connector;
	
	public JournalViewer(Connector connector, String file, String data){
		setTitle(file);
		this.file = file;
		textArea = new JTextArea();
		textArea.setColumns(40);
        textArea.setLineWrap(true);
        textArea.setRows(40);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(true);
        this.connector = connector;
        
        textArea.setText(data);
        JButton save = new JButton("Save");
        save.setActionCommand("Save");
        save.addActionListener(this);
        getContentPane().setLayout(new BorderLayout());
        
        JScrollPane jScrollPane1 = new JScrollPane(textArea);
        getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        getContentPane().add(save, BorderLayout.SOUTH);
		pack();
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("Save")){
			connector.setAction(Commands.SAVE);
			connector.sendString(file+"\n"+textArea.getText());
		}
	}
}
